package com.orange.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.orange.modules.bean.SessionUser;
import com.orange.util.ReturnCode;


/**
 * @author gaogs
 *
 * 2016-3-9
 */
public class LoginInterceptor implements HandlerInterceptor {

	private static final Logger logger = Logger.getLogger(LoginInterceptor.class);
	
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		response.setStatus(HttpStatus.OK.value());
		
		String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==pid){
			logger.info("---未登录---");
			response.getWriter().write("{\"code\":"+ReturnCode.NOLOGIN+",\"errorMsg\":\"未登录\",\"data\":\"FAILURE\"}");
			return false;
		}else{
			request.getSession().setAttribute(SessionUser.SESSION_USER_KEY, pid);
		}
		return true;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {

	}


}
